package com.easylinkin.sm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.easylinkin.sm.constant.LogConstant.Module;
import com.easylinkin.sm.context.AdminAccountContext;
import com.easylinkin.sm.entity.User;
import com.easylinkin.sm.service.LoginService;
import com.easylinkin.sm.service.RoleService;
import com.easylinkin.sm.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import site.morn.boot.data.CrudControllerSupport;
import site.morn.framework.entity.BaseUser;
import site.morn.log.OperateGroup;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * 登录控制器
 *
 * @author timely-rain
 * @since 1.0.0, 2019/8/15
 */
@Api(tags = "后台登录管理")
@OperateGroup(value = "admin", args = Module.LOGIN)
@RestController
@RequestMapping("/admin")
public class LoginController extends CrudControllerSupport<User, Long, LoginService> {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    /**
     * 获取当前用户信息
     *
     * @return REST消息
     */
    @ApiOperation("获取当前用户信息")
    @GetMapping("info")
    public RestMessage info() {
        BaseUser baseUser = AdminAccountContext.currentUser();
        User user = userService.findByUsername(baseUser.getUsername());
        JSONObject obj = new JSONObject();
        obj.put("user", user);
        obj.put("roles", roleService.getByUserId(user.getId()));
        obj.put("auth", null);
        
        return RestBuilders.successBuilder().data(obj).build();
    }
}
